package game.controller.gameEvent
{
	import game.constants.gameEvent.notif.GameEventNotification;
	import game.interfaces.IGameEvent;
	import game.model.gameEvent.GameEventProxy;
	import game.model.gameEvent.vo.GameEventVO;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	/**
	 * ...
	 * @author Thot
	 */
	
	public class LaunchGameEventCommand extends SimpleCommand
	{
		override public function execute( notification:INotification ):void
		{
			var proxy:GameEventProxy = facade.retrieveProxy(GameEventProxy.NAME) as GameEventProxy;
			var body:GameEventNotification = notification.getBody() as GameEventNotification;
			var trigger:String = body.trigger;
			var gameEvents:Vector.<IGameEvent> = proxy.getGameEventsByTrigger( trigger );
			
			var i:int = 0;
			var max:int = gameEvents.length;
			var gameEvent:IGameEvent;
			
			for ( i = 0; i < max; i++ )
			{
				gameEvent = gameEvents[i];
				
				if ( gameEvent.checkConditions() )
				{
					gameEvent.iteration--;
					sendNotification( gameEvent.actionName, gameEvent.actionBody );
					
					if ( gameEvent.iteration < 1 )
					{
						proxy.removeGameEvent(gameEvent);
						gameEvent.destroy();
					}
				}
			}
		}
	}

}